//
//  JYJAnchorColumnCollectionViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/2.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJAnchorColumnCollectionViewCell.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJAnchorColumnCollectionViewCellTwo.h"
#import "JYJSpecialListModel.h"
#import "JYJNetWorkParser.h"
#import "JYJAnchorColumnModel.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
@interface JYJAnchorColumnCollectionViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray <JYJAnchorColumnModel *> *marrOfModel;
@end
@implementation JYJAnchorColumnCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];
    }
    return self;
}
- (void)setListModel:(JYJSpecialListModel *)listModel {
    
    _listModel = listModel;
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22%22%2C%22sid%22%3A%22%22%7D%2C%22cat_id%22%3A%220%22%2C%22target_id%22%3A%22";
    NSString *string= [NSString stringWithFormat:@"%@",listModel.content_id];
    NSString *str3 = @"%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A2%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse: JYJJSON andSuccessBlock:^(id result) {
        self.marrOfModel = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        for (NSDictionary *dic in dataArray) {
            JYJAnchorColumnModel *model = [JYJAnchorColumnModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
        }
        [self.collection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)setDic:(NSDictionary *)dic {
    _dic = dic;
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22%22%2C%22sid%22%3A%22%22%7D%2C%22cat_id%22%3A%220%22%2C%22target_id%22%3A%22";
    NSString *string= [NSString stringWithFormat:@"%@",dic[@"item_managerid"]];
    NSString *str3 = @"%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A2%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse: JYJJSON andSuccessBlock:^(id result) {
        self.marrOfModel = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        for (NSDictionary *dic in dataArray) {
            JYJAnchorColumnModel *model = [JYJAnchorColumnModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
        }
        [self.collection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    

}
- (void)createViews {
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    self.collection = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[JYJAnchorColumnCollectionViewCellTwo class] forCellWithReuseIdentifier:@"Column"];
    self.collection.backgroundColor = [UIColor whiteColor];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 80) / 3, (SCREEN_WIDTH - 80) * 4 / 9);
    _layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    _layout.minimumLineSpacing = 20;
    _layout.minimumInteritemSpacing = 20;
    self.collection.frame = self.bounds;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.marrOfModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJAnchorColumnCollectionViewCellTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Column" forIndexPath:indexPath];
    JYJAnchorColumnModel *model = self.marrOfModel[indexPath.item];
    [cell setColumnModel:model];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfFeaturedTopThreeDetails *detailsOfVc = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
    JYJAnchorColumnModel *model = self.marrOfModel[indexPath.item];
    detailsOfVc.columnModel = model;
    self.handleAction(detailsOfVc);
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
